package com.app.medicalcontrol.login;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.widget.EditText;

import com.app.medicalcontrol.R;
import com.app.medicalcontrol.basis.BasisActivity;
import com.app.medicalcontrol.basis.Constants;
import com.app.medicalcontrol.bean.LoginBean;
import com.app.medicalcontrol.home.HomeTabActivity;
import com.app.medicalcontrol.jpush.TagAliasOperatorHelper;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import my.ActivityTool;
import my.MD5;
import my.MySharedPreferences;
import my.SystemParamsUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

public class LoginActivity extends BasisActivity{

    static final int HTTP_LOGIN = 12;

    @BindView(R.id.edit_user_name)
    public EditText mEtUserName;
    @BindView(R.id.edit_pwd)
    public EditText mEtPwd;

    MySharedPreferences mSp;

    public static void ToLogin(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivityForResult(intent, requestCode);

    }

    public static boolean toLoginIfNotLogin(Activity context, int requestCode) {
        if (!LoginBean.isLogin()) {
            ToLogin(context, requestCode);
            return false;
        }
        return true;
    }

    public static void ToLogin(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    public static void toLoadingAllClear(Context mContext) {
        LoginBean.logout();
        ActivityTool.skipActivityAndFinish(mContext,LoginActivity.class);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.activity_login);

        mEtUserName.setText("111111");
        mEtPwd.setText("111111");


        mSp = new MySharedPreferences(this);

//        if(!TextUtils.isEmpty(mSp.getUser())){
//            mEtUserName.setText(""+mSp.getUser());
//        }
//
//        if(!TextUtils.isEmpty(mSp.getPassword())){
//            mEtPwd.setText(""+mSp.getPassword());
//        }

    }

    String userName;
    String password;
    @OnClick(R.id.btn_login)
    public void login(){
       userName = mEtUserName.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            showToast(R.string.user_name_empty);
            return;
        }


        password = mEtPwd.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            showToast(R.string.pwd_empty);
            return;
        }

//        if(!TextLengthUtil.mixLength(password)){
//            showToast(R.string.pwd_length);
//            return;
//        }

        RequestParams params = new RequestParams();
        params.put("username", userName);
        params.put("password", MD5.getMD5ofStrLowercase(password));
//        params.put("equipment", "api-test");
        params.put("equipment", SystemParamsUtils.getUUID());
        showLoading();

        HttpRestClient.post( Constants.URL_USER_LOGIN, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                LoginBean loginBean = (LoginBean) result;
                onLoginSuccess(loginBean);
            }

            @Override
            public void onFinish(int httpWhat) {
                hideLoading();
            }
        }, HTTP_LOGIN, LoginBean.class);
    }


    protected void onLoginSuccess(LoginBean loginBean) {
        showToast("登录成功");
        loginBean.save();

        mSp.putUser(userName);
        if (!TextUtils.isEmpty(password)) {
            mSp.putPassword(password);
        }
        TagAliasOperatorHelper.getInstance().setAlias(LoginBean.getInstance().getJpushAlias());
        ActivityTool.skipActivityAndFinish(mContext,HomeTabActivity.class);
    }

}
